<?php

namespace app\data\service;

use think\admin\Service;
use think\facade\Log;

/**
 * 汽车模块服务
 * Class OrderService
 * @package app\data\service
 */
class CarService extends Service
{
    /**
     * 格式化年检记录
     * @param $inspectionRecord
     * @return array|null[]
     */
    public function formatInspectionRecord($inspectionRecord): array
    {
        // 使用正则表达式提取有效期部分
        $pattern = '/(\d+年\d+月)/u'; // 匹配数字开头，月结尾的部分
        preg_match($pattern, $inspectionRecord, $matches);

        if (!isset($matches[1])) {
            return [null, null];
        }

        $periodString = $matches[1];
        // 去除年份和月份中的非数字部分
        $periodString = preg_replace('/[^\d]/', '', $periodString);

        // 将提取的字符串转换为日期格式
        $year = substr($periodString, 0, 4);
        $month = substr($periodString, 4, 2);

        $year = intval($year);
        $month = intval($month);

        if (empty($year) || empty($month)) {
            return [null, null];
        }

        $date = \DateTime::createFromFormat('Ym', $year . $month);

        if ($date instanceof \DateTime) {
            $formattedDate = $date->format('Y-m-t');
        } else {
            $formattedDate = '';
        }

        return [$formattedDate, $month];
    }
}